package com.koron.order.linepatrol.satellite.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.StringUtils;
import com.koron.order.linedefect.base.bean.po.DefectBean;
import com.koron.order.linedefect.base.bean.query.DefectQuery;
import com.koron.order.linedefect.base.service.DefectService;
import com.koron.order.linepatrol.check.bean.po.LinePatrolCheckSatellite;
import com.koron.order.linepatrol.check.bean.query.LinePatrolCheckSatelliteQuery;
import com.koron.order.linepatrol.check.bean.vo.LinePatrolCheckSatelliteVO;
import com.koron.order.linepatrol.check.service.LinePatrolCheckSatelliteService;
import com.koron.order.linepatrol.satellite.bean.dto.LinePatrolMonitorChangeDTO;
import com.koron.order.linepatrol.satellite.bean.po.LinePatrolMonitorChange;
import com.koron.order.linepatrol.satellite.bean.query.LinePatrolMonitorChangeQuery;
import com.koron.order.linepatrol.satellite.mapper.LinePatrolMonitorChangeMapper;
import com.koron.order.linepatrol.satellite.service.LinePatrolMonitorChangeService;
import com.koron.order.risk.base.bean.query.LineRiskQuery;
import com.koron.order.risk.base.bean.vo.LineRiskVO;
import com.koron.order.risk.base.service.LineRiskService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 卫星监测变化工单(LinePatrolMonitorChange)表服务实现类
 *
 * @author jzp
 * @since 2023-02-08 16:47:14
 */
@Service
public class LinePatrolMonitorChangeServiceImpl implements LinePatrolMonitorChangeService {

    @Autowired
    LinePatrolCheckSatelliteService linePatrolCheckSatelliteService;

    @Autowired
    LineRiskService lineRiskService;

    @Autowired
    DefectService defectService;


    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public LinePatrolMonitorChange getById(SessionFactory factory, String id) {
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.selectById(id);
    }
    @Override
    @TaskAnnotation("selectDetailById")
    public LinePatrolMonitorChangeDTO selectDetailById(SessionFactory factory, String id) {
        LinePatrolMonitorChange linePatrolMonitorChange= getById(factory, id);
        LinePatrolMonitorChangeDTO linePatrolMonitorChangeDTO = new LinePatrolMonitorChangeDTO();
        linePatrolMonitorChangeDTO.setLinePatrolMonitorChange(linePatrolMonitorChange);
        if(StringUtils.isNotEmpty(linePatrolMonitorChange.getCheckId())){
            LinePatrolCheckSatelliteQuery linePatrolCheckSatelliteQuery = new LinePatrolCheckSatelliteQuery();
            linePatrolCheckSatelliteQuery.setId(linePatrolMonitorChange.getCheckId());
            List<LinePatrolCheckSatellite> list = linePatrolCheckSatelliteService.list(factory, linePatrolCheckSatelliteQuery);
            LinePatrolCheckSatellite checkSatellite = list.stream().findFirst().orElse(new LinePatrolCheckSatellite());
            linePatrolMonitorChangeDTO.setLinePatrolCheckSatellite(checkSatellite);
        }
        LineRiskQuery lineRiskQuery = new LineRiskQuery();
        lineRiskQuery.setPatrolId(linePatrolMonitorChange.getId());
        List<LineRiskVO> lineRiskVOS = lineRiskService.list(factory, lineRiskQuery);
        linePatrolMonitorChangeDTO.setLineRiskOrders(lineRiskVOS);
        DefectQuery defectQuery = new DefectQuery();
        defectQuery.setPatrolId(linePatrolMonitorChange.getId());
        List<DefectBean> defectBeans = defectService.queryList(factory, defectQuery);
        linePatrolMonitorChangeDTO.setDefectOrders(defectBeans);
        return linePatrolMonitorChangeDTO;
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<LinePatrolMonitorChange> list(SessionFactory factory, LinePatrolMonitorChangeQuery query) {
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("listByCheckId")
    public List<LinePatrolMonitorChange> listByCheckId(SessionFactory factory, String checkId) {
        LinePatrolMonitorChangeQuery linePatrolMonitorChangeQuery = new LinePatrolMonitorChangeQuery();
        linePatrolMonitorChangeQuery.setCheckId(checkId);
        return list(factory, linePatrolMonitorChangeQuery);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LinePatrolMonitorChange> page(SessionFactory factory, PageQuery pageQuery, LinePatrolMonitorChangeQuery query) {
        return LinePatrolMonitorChangeService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LinePatrolMonitorChange linePatrolMonitorChange) {
        if (linePatrolMonitorChange.getCheckStatus() == null) {
            linePatrolMonitorChange.setCheckStatus("0");
        }
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.insertOrUpdate(linePatrolMonitorChange) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<LinePatrolMonitorChange> linePatrolMonitorChangeList) {
        if (CollUtil.isEmpty(linePatrolMonitorChangeList)) {
            return false;
        }
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.insertOrUpdateBatch(linePatrolMonitorChangeList) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LinePatrolMonitorChange linePatrolMonitorChange) {
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.update(linePatrolMonitorChange) > 0;
    }


    @Override
    public boolean fillCheck(SessionFactory factory, LinePatrolCheckSatelliteVO linePatrolCheckSatelliteVO, List<String> patrolIds) {
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.fillCheck(linePatrolCheckSatelliteVO, patrolIds) > 0;
    }

    @Override
    public boolean clearCheck(SessionFactory factory, String checkId) {
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.clearCheck(checkId) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LinePatrolMonitorChangeMapper mapper = factory.getMapper(LinePatrolMonitorChangeMapper.class);
        return mapper.hidden(id) > 0;
    }
}
